
DIR_BUILD :=build
DIR_SOURCE :=src
DIR_INC :=inc
CFLAGS :=-Og -Wall -g
CLIBS :=-lpthread
CSOURCE :=$(wildcard src/*.c)
CSOURCE :=$(notdir $(CSOURCE))
OBJS :=$(CSOURCE:%.c=%.o);
DEPS :=$(patsubst %.c,%.d,$(CSOURCE))
TARGET :=a.out
OBJS:=$(addprefix $(DIR_BUILD)/,$(OBJS))
DEPS:=$(addprefix $(DIR_BUILD)/,$(DEPS))
#TARGET:=$(addprefix $(DIR_BUILD)/,$(TARGET))
all: $(TARGET)

ifeq ("$(MAKECMDGOALS)","")
include $(DEPS)
endif

ifeq ("$(MAKECMDGOALS)","all")
include $(DEPS)
endif



$(TARGET): $(OBJS)
	@echo "=> create target => $@"
	gcc $(CFLAGS) $(filter %.o,$^) -o $@


$(DIR_BUILD)/%.d: $(DIR_SOURCE)/%.c | $(DIR_BUILD)
	@echo "=> create depends file =>$@"
	gcc -MM -I$(DIR_INC) $(filter %.c,$^) | sed "s/\(.*\)\.o/build\/\1.d\ build\/\1.o/g" >$@


$(DIR_BUILD)/%.o: $(DIR_SOURCE)/%.c | $(DIR_BUILD)
	@echo "=> create compile filei =>$@"
	gcc -I$(DIR_INC) -c $(filter %.c,$^) $(CFLAGS) -o $@

$(DIR_BUILD):
	mkdir -p $@

.PHONY:print clean

print:
	@echo $(CSOURCE)
	@echo $(OBJS)
	@echo $(DEPS)

clean:
	@rm -rf $(DIR_BUILD)
	@echo "Target Clean...!"
	@rm -rf a.out